public class A2 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;

        int carry = 0;
        while (l1 != null || l2 != null) {
            // 获取值
            int value1 = l1 != null ? l1.val : 0;
            // 获取值
            int value2 = l2 != null ? l2.val : 0;
            // 包含之前的进位，这里可能还是0
            int sum = value1 + value2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            // 获取进位
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }

        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);

        ListNode l2 = new ListNode(2);
        l2.next = new ListNode(3);

        ListNode result = addTwoNumbers(l1, l2);
        System.out.println(result.val);
        System.out.println(result.next.val);
    }
}
